<?php
namespace Home\Model;
use Think\Model;
use Common\Common\Classes\Message;

class QuestionSetService extends Model{
    private $qs_sql;
    public function __construct(){
        $this->qs_sql = D("QuestionSet");
    }
    //---------------------------------获得某个班级的所有试卷---------------------------------
    public function getAllQuiz($np){
        $where["class_cla_id"] = $_GET["cad"];
        return $this->qs_sql->where($where)->limit(($np-1)*10,10)->order("qus_create_time desc")->select();
    }
    //---------------------------------获得某个班级的所有试卷(状态为1或者2的)---------------------------------
    public function getAllOpenQuiz(){
        $where["class_cla_id"] = $_GET["cad"];
        $where["qus_status"] = array("neq",0);
        $field = array("qus_id","qus_name","qus_create_time");
        return $this->qs_sql->where($where)->field($field)->order("qus_id desc")->select();
    }
    //----------------------------------------分页功能的实现---------------------------------
    public function getPageInfo(){
        $where["class_cla_id"] = $_GET["cad"];
        $pageInfo["count"] = $this->qs_sql->where($where)->count();
        $pageInfo['page'] = ceil(intval($pageInfo["count"]) / 10);
        $pageInfo['np'] = $_GET["np"];
        if($pageInfo['np'] <= 0 || $pageInfo['np'] > $pageInfo['page']){
            $pageInfo['np'] = 1;              //如果没有传当前页数过来就当第一页来处理
        }
        return $pageInfo;
    }
    //---------------------------------获得某个班级的所有试卷(状态为2的)---------------------------------
    public function getTwoOverQuiz(){
        $where["class_cla_id"] = $_POST["class_cla_id"];
        $where["qus_status"] = 2;
        $field = array("qus_id","qus_name");
        return $this->qs_sql->where($where)->order("qus_id desc")->field($field)->limit(0,2)->select();
    }
    //---------------------------------新增试卷------------------------------------
    public function addQuiz(){
        if(trim($_POST["qus_remark"])==""){
            $_POST["qus_remark"] = "如题所述";
        }
        if($this->qs_sql->create($_POST,1)){
            $this->qs_sql->add();
            $data = new Message(1,"新增试卷成功!","");
        }
        else{
            $data = new Message(0,$this->qs_sql->getError(),"");
        }
        return $data;
    }
    //---------------------------------修改试卷------------------------------------
    public function updateQuiz(){       //等待完成
        if(trim($_POST["qus_remark"])==""){
            $_POST["qus_remark"] = "如题所述";
        }
        if($this->qs_sql->create($_POST,2)){
            $where["qus_id"] = $_POST["qus_id"];
            $update["qus_name"] = $_POST["qus_name"];
            $update["qus_remark"] = $_POST["qus_remark"];
            $this->qs_sql->where($where)->save($update);
            $data = new Message(1,"试卷修改成功!","");
        }
        else{
            $data = new Message(0,$this->qs_sql->getError(),"");
        }
        return $data;
    }
    //---------------------------------删除试卷------------------------------------每一次只能删除一张试卷
    public function deleteQuiz(){
        $where["qus_id"] = $_POST["qus_id"];        //试卷的id是post过来的
        $field = array("qus_id");
        $check = $this->qs_sql->relation("deleteQuestion")->where($where)->field($field)->find();    //获得试卷的内容(主要是查看有木有题目)
        if(count($check["deleteQuestion"]) > 0){            //如果有题目的话会进入这一个删除题目的过程
            $que_id = array();
            for ($i = 0; $i < count($check["deleteQuestion"]); $i++) {      //整理获得所有要删除题目id
                $que_id[$i] = $check["deleteQuestion"][$i]["que_id"];
            }
            $que_sql = new QuestionService();
            $que_sql->RelationDeleteQuestion($que_id);         //联级删除题目以及相应的答题内容
        }//删除题目以及相应的答题内容已经完成!这个过程可能比较久!原因是可能有很多题目以及答题内容需要要删除(可能超过上百条数据需要删除)
        //联级删除试卷以及相应的试卷结果!
        $result = $this->qs_sql->relation("deleteResult")->where($where)->field($field)->delete();
        if($result >0){
            $data = new Message(1,"试卷删除成功!","");
        }
        else{
            $data = new Message(0,"试卷删除失败!","");        //基本不会失败!
        }
        return $data;
    }
    //---------------------------------检测试卷是否有题目或答题内容----------------------
    public function checkQuiz(){
        $where["qus_id"] = $_POST["qus_id"];
        $field = array("qus_id");       //数据不用多,只要有qus_id就够!省流量
        $checkQ = $this->qs_sql->relation("checkQuestion")->where($where)->field($field)->find();        //获得试卷的相关内容(题目关联)
        $question = $checkQ["checkQuestion"];        //获得该试卷的所有题目的数组
        if(count($question)!=0){        //检测题目数组的大小,不等于0表示拥有题目
            $data = new Message(1,"该试卷拥有题目,题目也会被删除,您确定删除吗?","");
        }
        else{
            $checkR = $this->qs_sql->relation("deleteResult")->where($where)->field($field)->find();        //获得试卷的相关内容(结果相关)
            $result = $checkR["deleteResult"];      //获得该试卷的所有结果的数组
            if(count($result)!=0){      //检测结果表数组大小,不等于0表示有成绩表
                $data = new Message(1, "该试卷拥有成绩表,成绩表也会被删除,您确定删除吗?", "");
            }
            else {
                $data = new Message(1, "您确定删除该试卷吗?", "");
            }
        }
        return $data;
    }
    //---------------------------------获得某个(其他)班级的所有试卷----------------------
    public function getOtherClassQuiz(){            //class_cla_id由post传过来
        $field = array("qus_id","qus_name","qus_remark");
        $quiz = $this->qs_sql->where($_POST)->field($field)->select();
        $data = new Message(1,"试卷查询成功!",$quiz);
        return $data;
    }
    //---------------------------------导入班级的试卷---------------------------------
    public function importQuiz(){           //以post形式.需要class_cla_id班级,qus_id需要导入的试卷id
        $where["qus_id"] = $_POST["qus_id"];                 //找到某张试卷的条件
        $field = array("qus_id");   //获得相应的字段(qus_id是必须的,不然关联不到)
        $import = $this->qs_sql->relation("importQuiz")->where($where)->field($field)->find();      //找到试卷的内容
        unset($import["qus_id"]);       //在导入试卷的时候用不到qus_id,因此销毁
        $import["qus_name"] = $_POST["qus_name"];
        $import["qus_remark"] = $_POST["qus_remark"];
        $import["class_cla_id"] = $_POST["class_cla_id"];    //将导入的试卷与班级关联起来
        if($this->qs_sql->create($import,1)){       //验证导入的数据是否没有问题(一般都是没有问题,这里只是用到自动填充为主)
            $result = $this->qs_sql->relation("importQuiz")->add($import);      //导入试卷
            if($result != ""){  //有返回数据表示成功
                $data = new Message(1,"导入".$import["qus_name"]."试卷成功!","");
            }
            else{
                $data = new Message(0,"导入".$import["qus_name"]."试卷失败!","");
            }
        }
        else{       //自动验证不过!(这种情况基本不可能出现)
            $data = new Message(0,$this->qs_sql->getError(),"");
        }
        return $data;
    }
    //---------------------------------获得某张试卷的基本信息-----------------------------------------------
    public function getQuizInfo($field){
        return $this->qs_sql->where($_POST)->field($field)->find();
    }
    //---------------------------------改变试卷的状态-----------------------------------------------
    public function changeStatusQuiz(){
        $field = array("qus_status");
        $result = $this->qs_sql->where($_POST)->field($field)->find();      //获得试卷当前的状态
        $qus_status = $result["qus_status"];
        if($qus_status == 0 || $qus_status == 2){           //未启用或已结束,要改成启用
            $update["qus_status"] = 1;
            $result = $this->qs_sql->where($_POST)->save($update);
            if($result>0){
                $data = new Message(1,"试卷启用生效!","");
            }
            else{
                $data = new Message(0,"试卷启用失败!","");
            }
        }
        else if($qus_status == 1){          //启用,要改成已结束
            $update["qus_status"] = 2;
            $result = $this->qs_sql->where($_POST)->save($update);
            if($result>0){
                $data = new Message(1,"试卷停用生效!","");
            }
            else{
                $data = new Message(0,"试卷停用失败!","");
            }
        }
        return $data;
    }
    //---------------------------------检测权限-----------------------------------------------
    public function checkPower(){
        $where["teacher_tea_id"] = $_SESSION["id"];
        $where["qus_id"] = $_POST["qus_id"];
        $result = D("CheckQuestionSetView")->where($where)->find();
        if($result != ""){
            $data = new Message(1,"你拥有权限对该试卷进行操作!","");
        }
        else{
            $data = new Message(0,"你没有权限对该试卷进行操作!","");
        }
        return $data;
    }

    public function __destruct(){}
}